import * as types from '../constants/ActionTypes';
import Request from '../utils/request';

/*
*销售机会列表接口
*/
const fetchSaleList =
(pageIndex, isLoading, isLoadMore, all, voicemsg, testdrive, collect, intention) =>
(dispatch) => {
  dispatch(fetchLoading(isLoading, isLoadMore));
  Request.get({
    url: `/dealer/users?n=${pageIndex}&&all=${all}&&testdrive=${testdrive}&&collect=${collect}&&intention=${intention}`,
    success: (data) => {
      if (data && data.users) {
        dispatch(saleList(data.users, data.count));
      }
    },
    fail: () => {
      dispatch(saleList([], 0));
    },
    error: () => {
      dispatch(saleList([], 0));
    },
  });
};

/*
*我的客户列表
*/
const fetchMyCustomerList =
(pageIndex, isLoading, isLoadMore, all, voicemsg, note, testdrive, collect,
  stageAll, cg, bj, sy, dt, cj, fq, qt, intention, visitAt) =>
(dispatch) => {
  dispatch(fetchMyLoading(isLoading, isLoadMore));
  Request.get({
    url: `/dealer/mycustomers?n=${pageIndex}&&all=${all}&&voicemsg=${voicemsg}&&note=${note}&&testdrive=${testdrive}&&collect=${collect}&&stage_all=${stageAll}&&cg=${cg}&&bj=${bj}&&sy=${sy}
      &&dt=${dt}&&cj=${cj}&&fq=${fq}&&intention=${intention}&&visit_at=${visitAt}`,
    success: (data) => {
      if (data && data.users) {
        dispatch(mycustomerList(data.users, data.count));
      }
    },
    fail: () => {
      dispatch(mycustomerList([], 0));
    },
    error: () => {
      dispatch(mycustomerList([], 0));
    },
  });
};

/**
*他人客户列表
*/
const fetchOtherList =
(pageIndex, isLoading, isLoadMore, all, voicemsg, testdrive, collect, intention) =>
(dispatch) => {
  dispatch(fetchOtherLoading(isLoading, isLoadMore));
  Request.get({
    url: `/dealer/other/customers?n=${pageIndex}&&all=${all}&&voicemsg=${voicemsg}&&testdrive=${testdrive}&&collect=${collect}&&intention=${intention}`,
    success: (data) => {
      if (data && data.users) {
        dispatch(otherList(data.users, data.count));
      }
    },
    fail: () => {
      dispatch(otherList([], 0));
    },
    error: () => {
      dispatch(otherList([], 0));
    },
  });
};

const saleList = (saleChanceData, saleListCount) => ({
  type: types.SALE_CHANCE_LIST,
  saleChanceData,
  saleListCount,
});
const mycustomerList = (myCustomerData, myCustomerListCount) => ({
  type: types.MY_CUSTOMER_LIST,
  myCustomerData,
  myCustomerListCount,
});
const otherList = (otherData, otherListCount) => ({
  type: types.OTHER_LIST,
  otherData,
  otherListCount,
});
const fetchLoading = (saleIsLoading, saleIsLoadMore) => ({
  type: types.SALE_CHANCE_LOADING,
  saleIsLoading,
  saleIsLoadMore,
});
const fetchMyLoading = (myIsLoading, myIsLoadMore) => ({
  type: types.MYCUSTOMER_LOADING,
  myIsLoading,
  myIsLoadMore,
});
const fetchOtherLoading = (otherIsLoading, otherIsLoadMore) => ({
  type: types.OTHER_LOADING,
  otherIsLoading,
  otherIsLoadMore,
});

export {
  fetchSaleList,
  fetchMyCustomerList,
  fetchOtherList,
  mycustomerList,
};
